<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>javascript-for-loop-send-ajax-request</title>
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head>

<body>

    <div id="result">

    </div>

    <script>
        var topicIds = ['5a54a8a4afa0a121784a8ab0',
            '5a2403226190c8912ebaceeb',
            '5a37c17dd92f2f5b185acfc4',
            '5a571ba39d371d4a059eea07',
            '5a4db120ebc575dc49b27108'
        ];

        /*
         * 使用forEach
         */
        topicIds.forEach(function (id, index, topicIds) {
            console.log(id);
            $.get('https://cnodejs.org/api/v1/topic/' + id, function (result) {
                console.log(id + '--' + index);
                console.log(result);
            })
        })

        for (var id of topicIds) {
            $.get('https://cnodejs.org/api/v1/topic/' + id, function (result) {
                console.log(id);
                console.log(result);
            })
        }

        /*
         * 立即执行函数
         */
        for (var id of topicIds) {
            (function (id) {
                $.get('https://cnodejs.org/api/v1/topic/' + id, function (result) {
                    console.log(id);
                    console.log(result);
                })
            }(id));
        }

        /*
         * 递归
         */
        function getTopicById(index, ids) {
            if (index < ids.length) {
                var id = ids[index];
                $.get('https://cnodejs.org/api/v1/topic/' + id, function (result) {
                    console.log(id);
                    console.log(result);
                    if (index < ids.length) {
                        getTopicById(index + 1, ids);
                    }
                })
            }
        }
        getTopicById(0, topicIds);


        /*
         * deferred
         */
        function getTopicByIdDefered(id) {
            var dtd = $.Deferred(); // 新建一个deferred对象
            $.get('https://cnodejs.org/api/v1/topic/' + id, function (result) {
                dtd.resolve({
                    id: id,
                    result: result
                });
            });
            return dtd.promise();
        }
        var getTopicByIdDeferedRequest = [];

        for (var id of topicIds) {
            getTopicByIdDeferedRequest.push(getTopicByIdDefered(id));
        }


        $.when(...getTopicByIdDeferedRequest).then(function (...result) {
            console.log(result);
        });

        Promise.all(getTopicByIdDeferedRequest).then(function (result) {
            console.log(result);
        });

        $.when.apply($, getTopicByIdDeferedRequest).then(function () {
            var objects = arguments; // The array of resolved objects as a pseudo-array
            console.log(arguments);
        });


        /*
         * promise
         */
        function getTopicByIdPromise(id) {
            return new Promise(function (resolve, reject) {
                $.get('https://cnodejs.org/api/v1/topic/' + id, function (result) {

                    resolve({
                        id: id,
                        result: result
                    });
                })
            })
        }

        var getTopicByIdPromiseArray=[];

        for (var id of topicIds) {
            getTopicByIdPromiseArray.push(getTopicByIdPromise(id));
        }

        Promise.all(getTopicByIdPromiseArray).then(function(result){
              console.log(result);
        });
    </script>
</body>

</html>